<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>居中</title>
    <style>
        *{
            margin: 0 auto;
            padding: 0;
            /* text-align: center; */
        }
/* 方法一 */
        .box1{
            width: 300px;
            height: 300px;
            background: firebrick;
            position: relative;
        }
        .content1{
            width: 100px;
            height: 100px;
            background: palevioletred;
            position: absolute;
            top: 50%;
            left: 50%;
            /* transform: translate(-50%,-50%); */
            transform: translate(-50px,-50px);
        }

/*方法二 */
        .box2{
            width: 300px;
            height: 300px;
            margin-top: 10px;
            background: yellow;
            position: relative;
        }
        .content2{
            width: 100px;
            height: 100px;
            background: orange;
            position: absolute;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
            /* margin: 0 auto; */
        }
/* 方法三 */
        .box3{
            width: 300px;
            height: 300px;
            background: brown;
            margin-top: 10px;
            display: flex;
            /* justify-content: center; */
            align-items: center;
        }
        .content3{
            width: 100px;
            height: 100px;
            background: pink;
        }
    </style>
</head>
<body>
    <!-- 第一种：利用父相子绝定位；transform：translate()移动到当前的宽高的一半 -->
    <div class="box1">
        <div class="content1"></div>
    </div>
    <!-- 第二种：利用flex布局 -->
    <div class="box2">
        <div class="content2"></div>
    </div>
    <!-- 第三种：flex布局 -->
    <div class="box3">
        <div class="content3"></div>
    </div>
</body>
</html>